// page/CardInfo/CardInfo.js
const http = require("../../../utils/http")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    useliist: [], //请求的客户信息列表
    useritem: '', //选择的客户信息
    indate: 1, //激活时间
    disabled: false, //立即激活if切换
    staffdata: [], //服务顾问列表
    fuwuguwen: '', //选择的服务顾问id
    listdata: null, //会员卡项目材料数据
    index: '', //服务顾问选择器顺序
    cardremainamount: 0, //面额
    discountsamount: 0, //优惠金额
    cardrealamount: 0, //实额
    textcont: '', //卡说明
    //userID:''//客户信息
    userimg: '', //用户图片
    focus: false, //搜索框焦点
    inputtext: '', //搜索框内容
    inputshow: true //输入框切换
  },
  add() { //提交
    if (this.data.useritem !== '' && this.data.fuwuguwen !== '' && this.data.cardrealamount !== '' && this.data.cardremainamount !== '') {
      http.postRequest('/mini/v1/vip/add_document', {
        owner_id: this.data.useritem.id, //选择的客户信息
        staff_id: this.data.fuwuguwen, //选择的服务顾问id
        card_tem_id: this.data.listdata.card_data.id, //卡模板id
        card_remain_amount: this.data.cardremainamount, //面额
        card_real_amount: this.data.cardrealamount, //实额
        activate_expires: this.data.indate, //激活期限 单位是【天】
        activate_status: this.data.indate == 0 ? '1' : '2', //开卡激活状态
        indate: this.data.listdata.card_data.indate, //有效期
        is_forever: this.data.listdata.card_data.is_forever //是否永久有效1===不是永久2===永久
      }, res => {
        wx.showToast({
          title: res.msg,
          duration: 1500,
        })

      })

    } else {
      wx.showToast({
        title: '请填完整',
        duration: 1500,
      })
    }
  },
  bindinputindate(e) { //激活时间
    console.log(e)
    this.setData({
      indate: e.detail.value

    })
  },
  textcontinput(e) { //卡说明

    this.setData({
      textcont: e.detail.value
    })
  },
  usetap(e) { //选择客户
    let userimg = '';
    if (e.currentTarget.dataset.item.gender == 2) { //男
      userimg = '/img/woman.png'
    } else { //女
      userimg = '/img/man.png'
    }
    this.setData({
      useritem: e.currentTarget.dataset.item,
      useliist: [],
      inputtext: '',
      inputshow: false,
      userimg: userimg
    })
  },
  inputblur(e) {
    this.setData({
      inputtext: ''
    })
  },
  bindinput(e) { //搜索客户
    if (e.detail.value == '') {
      this.setData({
        useliist: []
      })
    } else {
      http.postRequest('/mini/v1/car_owner/owner_list', {
        per_page: 10,
        page: 1,
        search: e.detail.value
      }, res => {
        let {
          data
        } = res.data
        this.setData({
          useliist: data
        })
      })
    }

  },
  yanchi() { //延迟激活切换
    this.setData({
      indate: 0,
      yancinun: !this.data.yancinun,
    })
  },
  anount(e) { //数值计算
    this.setData({
      discountsamount: parseInt(e.detail.value),
    })

    this.setData({


      cardremainamount: parseInt(this.data.discountsamount) + parseInt(this.data.cardrealamount)
    })
  },


  bindPickerChange: function (e) { //服务顾问选择器
    this.setData({
      index: e.detail.value,
      fuwuguwen: this.data.staffdata[e.detail.value].id
    })
  },


  formSubmit(e) {
    console.log(e)
  },
  // 显示遮罩层
  showModal: function () {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  // 隐藏遮罩层
  hideModal: function () {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  jumpVip() {
    wx.navigateTo({
      url: '/pages/openCard/vipSingle/vipSingle',
    })
  },
  jumpOrdin() {
    this.showModal()
  },
  closeModal() {
    this.hideModal()
  },
  jumpNewUser() { //清除客户信息，重新获取文本框焦点
    this.setData({
      useritem: '', //清空客户信息
      focus: true, //搜索框焦点
      inputshow: true
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { //服务顾问获取
    if (options.mobile) {
      let userimg = '';
      http.postRequest('/mini/v1/car_owner/owner_list',{search: options.mobile}, res=> {
        if (res.data.data[0].gender == 1) { //女
          userimg = '/img/woman.png'
        } else { //男
          userimg = '/img/man.png'
        }
        this.setData({
          useritem: res.data.data[0],
          useliist: [],
          inputtext: '',
          inputshow: false,
          userimg: userimg
        })
      })
    }
    http.postRequest('/mini/v1/vip/base_data', {}, res => {
      let {
        data
      } = res
      this.setData({
        staffdata: data.staff_data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { //会员卡项目材料数据 
    if (this.data.id && this.data.id !== '') {
      http.postRequest('/mini/v1/card/info', {
        card_id: this.data.id
      }, res => {
        console.log(res)
        let {
          data
        } = res
        this.setData({
          listdata: data,
          cardrealamount: data.card_data.card_real_amount, //卡实额度
          cardremainamount: data.card_data.card_remain_amount, //面额
          textcont: data.card_data.explain, //卡说明
          indate: data.card_data.activate_expires || 0 //激活时间
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})